package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.nursing.domain.CheckIn;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.vo.NursingTaskVo;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 护理任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper,NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private NursingElderMapper nursingElderMapper;
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ElderMapper elderMapper;
    /**
     * 查询护理任务
     * 
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTaskVo selectNursingTaskById(Long id)
    {
        NursingTask nursingTask = getById(id);
        NursingTaskVo nursingTaskVo = BeanUtil.toBean(nursingTask, NursingTaskVo.class);
        nursingTaskVo.setNursingName(nursingElderMapper.selectUsersByElderId(nursingTaskVo.getElderId()));
        Elder elder = elderMapper.selectElderById(nursingTask.getElderId());
        CheckIn checkIn = checkInMapper.selectOne(Wrappers.<CheckIn>lambdaQuery().eq(CheckIn::getElderId, elder.getId()));
        nursingTaskVo.setSex(elder.getSex());
        nursingTaskVo.setNursingLevelName(checkIn.getNursingLevelName());
        nursingTaskVo.setAge(IdcardUtil.getAgeByIdCard(elder.getIdCardNo()));
        String updateBy = nursingTaskVo.getUpdateBy();
        if (ObjectUtil.isNotEmpty(updateBy)){
            nursingTaskVo.setUpdater(sysUserMapper.selectUserById(Long.valueOf(updateBy)).getNickName());
        }
        return nursingTaskVo;
    }

    /**
     * 查询护理任务列表
     * 
     * @param nursingTaskDto 护理任务
     * @return 护理任务
     */
    @Override
    public List<NursingTask> selectNursingTaskList(NursingTaskDto nursingTaskDto)
    {
        return nursingTaskMapper.selectNursingTaskList(nursingTaskDto);
    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask)?1:0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask)?1:0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id)?1:0;
    }

}
